Belgelerim/ceng_491/implementation/doxygen/TraffEdu_Developers_Manual_Reference_ToSource/Road.cpp

Go to the documentation of this file.
00001 // road.cpp : Defines the entry point for the console application.
00002 
00003 
00004 #include "Road.h"
00005 
00006 RoadMap *theRoadMap;
00007 Road::Road(RoadType theType)
00008 {
00009         verts=new osg::Vec3Array;
00010         numControlPoints = 0;
00011         numPoints = 0;
00012         controlPointsIndex=0;
00013         dc=new LinearConstraint();
00014         dotRoot=new osg::Group;
00015         lineRoot=new osg::Group;
00016         type=theType;
00017         setWidthAndHeight();
00018 }
00019 void Road::setWidthAndHeight()
00020 {
00021         switch(type)
00022         {
00023         case TWO_HALFHALF_HALFHALF:
00024                 length = 5;
00025                 width = 11;
00026                 break;
00027         case ONE_HALF:
00028                 length = 5;
00029                 width = 6;
00030                 break;
00031         default:
00032                 length = 5;
00033                 width  = 6;
00034                 break;
00035         }
00036 }
00037 
00038 
00039 
00040 
00041 //Calculates the coordinates of points int between given 4 points
00042 //calculateCatmull
00043 osg::Vec3 Road::calculateCatmull(osg::Vec3 A, osg::Vec3 B, osg::Vec3 C,osg::Vec3 D, double t) {
00044         osg::Vec3 P;
00045 
00046         
00047         P.x() = 0.5 *((2 * B.x()) +
00048                                 (-A.x() + C.x()) * t +
00049         (2*A.x() - 5*B.x() + 4*C.x() - D.x()) * pow(t,2) +
00050         (-A.x() + 3*B.x() - 3*C.x() + D.x()) * pow(t,3));
00051 
00052         P.y() = 0.5 *((2 * B.y()) +
00053                                 (-A.y() + C.y()) * t +
00054         (2*A.y() - 5*B.y() + 4*C.y() - D.y()) * pow(t,2) +
00055         (-A.y() + 3*B.y() - 3*C.y() + D.y()) * pow(t,3));
00056 
00057         P.z() = 0.0;
00058                 return P;
00059 }
00060 
00061 
00062 
00063 //whe a new control point arives this function is called.
00064 //setPoints
00065 void Road::setPoints(void)
00066 {
00067 
00068         if(numControlPoints > 3) {
00069                 
00070                 for(double t = 0.0;t <= 1.0; t += 0.1) {
00071                         osg::Vec3 P = calculateCatmull(controlPointArray[controlPointsIndex], controlPointArray[controlPointsIndex+1], controlPointArray[controlPointsIndex+2],controlPointArray[controlPointsIndex+3], t);
00072                         verts->push_back( P);
00073                         numPoints ++;
00074                 }
00075 
00076                 controlPointsIndex ++;
00077                 
00078         }
00079                 
00080 }
00081 
00082 //display lines
00083 
00084 void Road::addLines(void)
00085 {
00086         osg::Geode* lineGeode = new osg::Geode();
00087         osg::ref_ptr<osg::Vec4Array> color = new osg::Vec4Array;
00088         color->push_back(osg::Vec4(0.0f,0.0f,0.0f,1.0f));
00089 
00090         osg::ref_ptr<osg::Vec3Array> normal = new osg::Vec3Array;
00091         normal->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
00092                         
00093         osg::Geometry* lineGeom = new osg::Geometry();
00094         
00095                 
00096         // pass the created vertex array to the points geometry object.
00097         lineGeom->setVertexArray(verts);
00098         
00099         // use the shared color array.
00100         lineGeom->setColorArray(color.get());
00101         lineGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
00102         
00103 
00104         // use the shared normal array.
00105         lineGeom->setNormalArray(normal.get());
00106         lineGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);
00107  
00108         
00109 
00110         // This time we simply use primitive, and hardwire the number of coords to use 
00111         // since we know up front,
00112                 
00113                 lineGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP,0,numPoints));
00114                 osg::StateSet* stateset = new osg::StateSet;
00115                 osg::LineWidth* linewidth = new osg::LineWidth();
00116             linewidth->setWidth(4.0f);
00117                 stateset->setAttributeAndModes(linewidth,osg::StateAttribute::ON);
00118                 stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
00119                 lineGeom->setStateSet(stateset);
00120                 lineGeode->addDrawable(lineGeom);
00121                 lineRoot->removeChild(0,1);
00122                 lineRoot->addChild(lineGeode);
00123 }
00124 
00125 void Road::addDots()
00126 {
00127         osg::Geode* dotGeode = new osg::Geode();
00128         osg::ref_ptr<osg::Vec4Array> color = new osg::Vec4Array;
00129         color->push_back(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
00130 
00131         osg::ref_ptr<osg::Vec3Array> normal = new osg::Vec3Array;
00132         normal->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
00133                         
00134         osg::Geometry* dotGeom = new osg::Geometry();
00135         
00136         osg::Vec3Array* vertices = new osg::Vec3Array(numControlPoints,controlPointArray);
00137         
00138     // pass the created vertex array to the points geometry object.
00139     dotGeom->setVertexArray(vertices);
00140     
00141     // use the shared color array.
00142     dotGeom->setColorArray(color.get());
00143     dotGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
00144     
00145 
00146     // use the shared normal array.
00147     dotGeom->setNormalArray(normal.get());
00148     dotGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);
00149     
00150 
00151     // This time we simply use primitive, and hardwire the number of coords to use 
00152     // since we know up front,
00153         dotGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS,0,numControlPoints));
00154         dotGeode->addDrawable(dotGeom);
00155         osg::StateSet *set = new osg::StateSet();
00156         osg::Point *point = new osg::Point();
00157         float size=3.0;
00158     point->setSize(size);
00159     set->setAttribute(point);
00160         dotGeode->setStateSet(set);
00161         dotRoot->removeChild(0,1);
00162         dotRoot->addChild(dotGeode);
00163         return;
00164 }
00165 
00166 
00167 
00168 
00169 void Road::addNewPoint( osg::Vec3 pos )
00170 {
00171         
00172         controlPointArray[numControlPoints] = pos;
00173         controlPointArray[numControlPoints][2] = 0.0;//1.5+0.01;
00174         
00175         numControlPoints++;
00176         setPoints();
00177         addDots();
00178         addLines();
00179 }
00180 
00181 string* Road::returnTextName()
00182 {
00183         switch(type)
00184         {
00185                 case TWO_HALFHALF_HALFHALF:
00186                         return new string("Data/road/TWO_HALFHALF_HALFHALF.png");
00187                 case ONE_HALF:
00188                         return new string("Data/road/ONE_HALF.png");
00189                 default:
00190                         return new string("Data/road/bosyol.png");
00191         }
00192 }
00193 
00194 void Road::geometryInitialize()
00195 {
00196         dc->setTexture (returnTextName()->c_str());
00197         dc->setTexrep(length, width);
00198         dc->setVertices(verts,width);
00199 }
00200 //  RoadMap Class 
00201 
00202 //According to the returned values from this function, points array is filled.
00203 
00204 osg::Vec3d RoadMap::getpt(const int np)
00205 { 
00206         // returns the x,y,height of terrain up to maxp^2 points
00207     static int maxp =40;
00208     int i=np/maxp;
00209     int j=np%maxp;
00210     // make the random scale 0.00 if you want an equispaced XY grid.
00211 
00212     float x=GROUND_SIZE/(maxp-1)*i-GROUND_SIZE/2;
00213     float y=GROUND_SIZE/(maxp-1)*j-GROUND_SIZE/2;
00214     float z=0.0;
00215     if (np>=maxp*maxp) z=-1.e32;
00216     return osg::Vec3d(x,y,z);
00217 }
00218 
00219 void RoadMap::pinControl(osg::ref_ptr<osg::Group> sgroot)
00220 {
00221         if (pinFlag==0)
00222         {
00223                 pinFlag=1;
00224                 sgroot->addChild(linedotRoot.get());
00225         }
00226         else 
00227         {
00228                 pinFlag=0;
00229                 sgroot->removeChild(linedotRoot.get());
00230         }
00231         return;
00232 }
00233 
00234 void RoadMap::loadRoadScene()
00235 {
00236         static int t=0;
00237         osg::ref_ptr<osg::Node> loadedModel = makedelaunay();
00238         osgUtil::Optimizer optimizer;
00239         optimizer.optimize(loadedModel.get());
00240         if(t) 
00241         {
00242                 int ij=roadRoot->getNumChildren();
00243                 for(int i=0;i<ij;i++)
00244                         roadRoot->removeChild(0,1);
00245         }
00246         roadRoot->addChild(roadFile.get());
00247         roadRoot->addChild(loadedModel.get());
00248         t++;
00249 }
00250 
00251 //Picking, addnewPoint
00252 RoadMap::RoadMap(osg::ref_ptr<osg::Group> sgroot)
00253 {
00254         
00255         dotRootNode=new osg::Group;
00256         lineRootNode=new osg::Group;
00257         roadRoot=new osg::Group;
00258         roadFile=new osg::Group;
00259         linedotRoot=new osg::Group;
00260         sgroot->addChild(roadRoot.get());
00261         linedotRoot->addChild(dotRootNode.get());
00262         linedotRoot->addChild(lineRootNode.get());
00263         sgroot->addChild(linedotRoot.get());
00264         currentRoadIndex=-1;
00265         lastRoadIndex=-1;
00266         pinFlag=1;
00267         flagForFile=0;
00268 }
00269 
00270 void RoadMap::addRoadMaptoRoot(osg::ref_ptr<osg::Group> sgroot, int flag)
00271 {
00272         sgroot->addChild(roadRoot.get());
00273         if(flag) sgroot->addChild(linedotRoot.get());
00274 }
00275 
00276 
00277 void RoadMap::addNewRoad(RoadType type)
00278 {
00279         roadMap.push_back(new Road(type));
00280         lastRoadIndex++;
00281         dotRootNode->addChild(roadMap[lastRoadIndex]->dotRoot.get());
00282         lineRootNode->addChild(roadMap[lastRoadIndex]->lineRoot.get());
00283 }
00284 
00285 
00286 void RoadMap::addPoint (osg::Vec3 position)
00287 {
00288         roadMap[currentRoadIndex]->addNewPoint(position);
00289 }
00290 
00291 void RoadMap::setActiveRoadIndex(int index)
00292 {
00293         currentRoadIndex=index;
00294 }
00295 
00296 int RoadMap::getActiveRoadIndex()
00297 {
00298         return currentRoadIndex;
00299 }
00300 
00301 int RoadMap::getLastRoadIndex()
00302 {
00303         return lastRoadIndex;
00304 }
00305 
00306 void RoadMap::addRoadNode(osg::ref_ptr<osg::Node> roadNodeToBeAdded) 
00307 {
00308         int ij=roadRoot->getNumChildren();
00309         for(int i=0;i<ij;i++)
00310                 roadRoot->removeChild(0,1);
00311         roadFile->addChild(roadNodeToBeAdded.get());
00312         roadRoot->addChild(roadFile.get());
00313 }
00314 
00315 void RoadMap::clearRoadMap()
00316 {
00317         // clear roadRoot
00318         int mapSize= (int)roadMap.size();
00319         for(int i=0; i<mapSize; i++)
00320                 roadMap.pop_back();
00321         osg::ref_ptr<osg::Node> loadedModel = makedelaunay();
00322         osgUtil::Optimizer optimizer;
00323         optimizer.optimize(loadedModel.get());
00324         int ij=roadRoot->getNumChildren();
00325         for(int i=0;i<ij;i++)
00326                 roadRoot->removeChild(0,1);
00327         roadRoot->addChild(loadedModel.get());
00328 
00329         //clear lines and dots
00330         ij=lineRootNode->getNumChildren();
00331         for(int i=0;i<ij;i++)
00332                 lineRootNode->removeChild(0,1);
00333         ij=dotRootNode->getNumChildren();
00334         for(int i=0;i<ij;i++)
00335                 dotRootNode->removeChild(0,1);
00336 
00337         currentRoadIndex=-1;
00338         lastRoadIndex=-1;
00339 
00340 }
00341 osg::Group * RoadMap::makedelaunay()
00342 {
00343          osg::ref_ptr<osg::Group> grp=new osg::Group;
00344     osg::ref_ptr<osg::Geode> geode=new osg::Geode;
00345     osg::ref_ptr<osgUtil::DelaunayTriangulator> trig=new osgUtil::DelaunayTriangulator();
00346     osg::StateSet *stateset=geode->getOrCreateStateSet();
00347         osg::ref_ptr<osgUtil::DelaunayConstraint> dcoverlap=new osgUtil::DelaunayConstraint();
00348     osg::Vec3Array *points=new osg::Vec3Array;
00349         
00350     osg::Image* image = osgDB::readImageFile("Data/road/fig6.png");
00351     if (image)
00352     {
00353         osg::Texture2D* texture = new osg::Texture2D;
00354         texture->setImage(image);
00355         texture->setWrap( osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT );
00356         texture->setWrap( osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT );
00357         stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
00358     }
00359     
00360     geode->setStateSet( stateset );
00361         unsigned int i;
00362     
00363     int eod=0;
00364     while (eod>=0) {
00365         osg::Vec3d pos=getpt(eod);
00366         if (pos.z()>-10000) {
00367             points->push_back(pos);
00368             eod++;
00369         } else {
00370             eod=-9999;
00371         }
00372     }
00373         int size = (int)roadMap.size();
00374         for(int i=0;i<size;i++)
00375         {
00376                 if(roadMap[i]->numControlPoints > 3)
00377                 {
00378                         roadMap[i]->dc=new LinearConstraint();
00379                         roadMap[i]->geometryInitialize();
00380                         dcoverlap->merge(roadMap[i]->dc.get());
00381                 }
00382         }
00383         dcoverlap->handleOverlaps();
00384 
00385         size=(int)roadMap.size();
00386         for(int i=0;i<size;i++)
00387         {
00388                 if(roadMap[i]->numControlPoints > 3)
00389                 {
00390                         dcoverlap->removeVerticesInside(roadMap[i]->dc.get());  
00391                 }
00392         }
00393         for (osg::Vec3Array::iterator vit=points->begin(); vit!=points->end(); ) {
00394                                         if (dcoverlap->contains(*vit)) {
00395                                             vit=points->erase(vit);
00396                                         } else {
00397                                             vit++;
00398                                         }
00399                                     }
00400         trig->addInputConstraint(dcoverlap.get());
00401         trig->setInputPointArray(points);
00402         osg::Vec3Array *norms=new osg::Vec3Array;
00403     trig->setOutputNormalArray(norms);
00404         trig->triangulate();
00405         // Calculate the texture coordinates after triangulation as 
00406     //the points may get disordered by the triangulate function
00407     osg::ref_ptr<osg::Geometry> gm=new osg::Geometry;
00408     gm->setVertexArray(points); // points may have been modified in order by triangulation.
00409     // * calculate texture coords for terrain points 
00410     if (image) {
00411         float repeat=150.0, ry=150.0; // how often to repeat texture
00412         osg::Vec2Array *tcoords=new osg::Vec2Array;
00413         for (osg::Vec3Array::iterator itr=points->begin(); itr!=points->end(); itr++) {
00414             osg::Vec2 tcatxy((*itr).x()/repeat,(*itr).y()/ry);
00415             tcoords->push_back(tcatxy);
00416         }
00417         gm->setTexCoordArray(0,tcoords);
00418     }
00419     gm->addPrimitiveSet(trig->getTriangles());
00420     gm->setNormalArray(trig->getOutputNormalArray());
00421     gm->setNormalBinding(osg::Geometry::BIND_PER_PRIMITIVE);
00422     geode->addDrawable(gm.get());
00423         for(int i=0;i<(int)roadMap.size();i++)
00424         {
00425                 if(roadMap[i]->numControlPoints > 3)
00426                 {
00427                         trig->removeInternalTriangles(roadMap[i]->dc.get());
00428                         roadMap[i]->geometryInitialize();
00429                         geode->addDrawable(roadMap[i]->dc->makeGeometry(points));
00430                 }
00431         }
00432         grp->addChild(geode.get());
00433         return grp.release();
00434 
00435 }

Generated on Sun May 28 18:54:09 2006 for TraffEdu by  doxygen 1.4.6-NO